/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Certification;
import com.applicatioN.entities.Exiger3;
import com.applicatioN.entities.Exiger3PK;
import com.applicatioN.entities.Poste;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class Exiger3JpaController implements Serializable {

    public Exiger3JpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Exiger3 exiger3) throws PreexistingEntityException, Exception {
        if (exiger3.getExiger3PK() == null) {
            exiger3.setExiger3PK(new Exiger3PK());
        }
        exiger3.getExiger3PK().setOrganisme(exiger3.getCertification().getCertificationPK().getOrganisme());
        exiger3.getExiger3PK().setDomaineDExpertise(exiger3.getCertification().getCertificationPK().getDomaineDExpertise());
        exiger3.getExiger3PK().setNomPoste(exiger3.getPoste().getNomPoste());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Certification certification = exiger3.getCertification();
            if (certification != null) {
                certification = em.getReference(certification.getClass(), certification.getCertificationPK());
                exiger3.setCertification(certification);
            }
            Poste poste = exiger3.getPoste();
            if (poste != null) {
                poste = em.getReference(poste.getClass(), poste.getNomPoste());
                exiger3.setPoste(poste);
            }
            em.persist(exiger3);
            if (certification != null) {
                certification.getExiger3Collection().add(exiger3);
                certification = em.merge(certification);
            }
            if (poste != null) {
                poste.getExiger3Collection().add(exiger3);
                poste = em.merge(poste);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findExiger3(exiger3.getExiger3PK()) != null) {
                throw new PreexistingEntityException("Exiger3 " + exiger3 + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Exiger3 exiger3) throws NonexistentEntityException, Exception {
        exiger3.getExiger3PK().setOrganisme(exiger3.getCertification().getCertificationPK().getOrganisme());
        exiger3.getExiger3PK().setDomaineDExpertise(exiger3.getCertification().getCertificationPK().getDomaineDExpertise());
        exiger3.getExiger3PK().setNomPoste(exiger3.getPoste().getNomPoste());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exiger3 persistentExiger3 = em.find(Exiger3.class, exiger3.getExiger3PK());
            Certification certificationOld = persistentExiger3.getCertification();
            Certification certificationNew = exiger3.getCertification();
            Poste posteOld = persistentExiger3.getPoste();
            Poste posteNew = exiger3.getPoste();
            if (certificationNew != null) {
                certificationNew = em.getReference(certificationNew.getClass(), certificationNew.getCertificationPK());
                exiger3.setCertification(certificationNew);
            }
            if (posteNew != null) {
                posteNew = em.getReference(posteNew.getClass(), posteNew.getNomPoste());
                exiger3.setPoste(posteNew);
            }
            exiger3 = em.merge(exiger3);
            if (certificationOld != null && !certificationOld.equals(certificationNew)) {
                certificationOld.getExiger3Collection().remove(exiger3);
                certificationOld = em.merge(certificationOld);
            }
            if (certificationNew != null && !certificationNew.equals(certificationOld)) {
                certificationNew.getExiger3Collection().add(exiger3);
                certificationNew = em.merge(certificationNew);
            }
            if (posteOld != null && !posteOld.equals(posteNew)) {
                posteOld.getExiger3Collection().remove(exiger3);
                posteOld = em.merge(posteOld);
            }
            if (posteNew != null && !posteNew.equals(posteOld)) {
                posteNew.getExiger3Collection().add(exiger3);
                posteNew = em.merge(posteNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Exiger3PK id = exiger3.getExiger3PK();
                if (findExiger3(id) == null) {
                    throw new NonexistentEntityException("The exiger3 with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Exiger3PK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exiger3 exiger3;
            try {
                exiger3 = em.getReference(Exiger3.class, id);
                exiger3.getExiger3PK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The exiger3 with id " + id + " no longer exists.", enfe);
            }
            Certification certification = exiger3.getCertification();
            if (certification != null) {
                certification.getExiger3Collection().remove(exiger3);
                certification = em.merge(certification);
            }
            Poste poste = exiger3.getPoste();
            if (poste != null) {
                poste.getExiger3Collection().remove(exiger3);
                poste = em.merge(poste);
            }
            em.remove(exiger3);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Exiger3> findExiger3Entities() {
        return findExiger3Entities(true, -1, -1);
    }

    public List<Exiger3> findExiger3Entities(int maxResults, int firstResult) {
        return findExiger3Entities(false, maxResults, firstResult);
    }

    private List<Exiger3> findExiger3Entities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Exiger3.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Exiger3 findExiger3(Exiger3PK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Exiger3.class, id);
        } finally {
            em.close();
        }
    }

    public int getExiger3Count() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Exiger3> rt = cq.from(Exiger3.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
